//------------------------------------------------------------------------------
#include <QSqlRecord>
#include <QSqlQuery>
#include <QtDebug>
#include "CPontComptable.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
void CLignePont::add(double value)
{
	_montant+=value;
}
//------------------------------------------------------------------------------
CFacturePontComptable::CFacturePontComptable(int numFacture, QString compteClient, QString nomClient, QDate creation, QDate echeance, QString document)
{
	_numFacture=numFacture; 
	_compteClient=compteClient;
	_nomClient=nomClient;
	_creation=creation; 
	_echeance=echeance; 
	_document=document;
	
	lignesPont.insert(compteClient, new CLignePont(compteClient));
}
//------------------------------------------------------------------------------
void CFacturePontComptable::addMontant(QString compte, double montant)
{
	if(montant != 0)
	{
		CLignePont *lignePont;
		
		if(lignesPont.contains(compte))
		{
			lignePont=lignesPont.value(compte);
		}else 
		{
			lignePont=new CLignePont(compte);
			lignesPont.insert(compte, lignePont);
		}
		
		if(_document == "F") 
		{
			lignePont->add(montant);
			lignesPont.value(_compteClient)->add(-montant);
		}else
		{
			lignePont->add(-montant);
			lignesPont.value(_compteClient)->add(montant);
		}
	}
}
//------------------------------------------------------------------------------
QList<CLignePont *> CFacturePontComptable::getLignesPont(void) const
{
	return lignesPont.values();
}
//------------------------------------------------------------------------------
void CPontComptable::create(void)
{
	QString requete;
	QSqlQuery *query;
	
	requete="SELECT c.compteComptable AS CCC, c.nom, tf.code, tf.creation, tf.echeance, tf.document, ";
	requete+="@mnt:=lf.prixUnitaireHT*lf.quantite AS MONTANT, @mnt*lf.tauxTva AS TVA, ";
	requete+="IF(tf.pays = 'FRANCE', fa.compteComptable, fa.compteExport) AS FCC, tc.compteComptable AS TCC ";
	requete+="FROM teteFactures AS tf ";
	requete+="INNER JOIN client AS c ON c.code=tf.codeClient ";
	requete+="INNER JOIN ligneFactures AS lf ON lf.facture=tf.code ";
	requete+="INNER JOIN article AS a ON a.code=lf.article ";
	requete+="INNER JOIN familleArticle AS fa ON fa.code=a.famille ";
	requete+="INNER JOIN tvaClient AS tc ON tc.idTva=a.idTva AND tc.codeClient=tf.codeClient ";
	requete+="WHERE idPont="+QString::number(_numPont)+" ";
	requete+="ORDER BY tf.code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query)) 
	{
		QSqlRecord rec;
		CFacturePontComptable *facture;
		
		while(query->next())
		{
			rec=query->record();
			int numFacture=rec.value("code").toInt();
			
			if(factures.contains(numFacture))
			{
				facture=factures.value(numFacture);
			}else 
			{
				facture=new CFacturePontComptable(numFacture, rec.value("CCC").toString(), rec.value("nom").toString(), rec.value("creation").toDate(), rec.value("echeance").toDate(), rec.value("document").toString());
				factures.insert(numFacture, facture);
			}
			
			facture->addMontant(rec.value("FCC").toString(), rec.value("MONTANT").toDouble());
			facture->addMontant(rec.value("TCC").toString(), rec.value("TVA").toDouble());
		}
	}
	
	delete query;
}
//------------------------------------------------------------------------------
void CPontComptable::save(void)
{
	CFacturePontComptable *facture;
	QString baseRequete, baseRequeteFacture, requete;
	QList<CLignePont *> lignesPont;
	
	baseRequete="INSERT INTO lignePontComptable (idPont, facture, dateFacture, dateEcheance, libelle, document, compte, debit, credit) SELECT ";
	for(int i=0;i<factures.size();i++)
	{
		facture=factures.values().at(i);
		lignesPont=facture->getLignesPont();
		
		baseRequeteFacture=baseRequete;
		baseRequeteFacture+=QString::number(_numPont)+", "+QString::number(facture->getNumFacture())+", ";
		baseRequeteFacture+="\""+facture->getCreation().toString("yyyy-MM-dd")+"\", \""+facture->getEcheance().toString("yyyy-MM-dd")+"\", ";
		baseRequeteFacture+="\""+facture->getNomClient()+"\", \""+facture->getDocument()+"\", ";
		
		for(int j=0;j<lignesPont.size();j++)
		{
			CLignePont *lignePont=lignesPont.at(j);
			
			requete=baseRequeteFacture;
			requete+="\""+lignePont->getCompte()+"\", "+QString::number(lignePont->getDebit(), 'f', 2)+", "+QString::number(lignePont->getCredit(), 'f', 2);
			
			CMySQL::getInstance()->execQuery(requete);
		}
	}
}
//------------------------------------------------------------------------------